package com.yiphonenum.phonenumberapplication;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AuthActivity extends AppCompatActivity {
    private long currentBackPressedTime = 0;
    private String _imei;
    protected int length_of_license = 7;  // 授权码长度

    TextView txtCode;
    EditText edAuthcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);

        txtCode = (TextView) findViewById(R.id.txtCode);
        edAuthcode = (EditText)findViewById(R.id.edtAuthCode);

        get_imei();
        // 写特征码
        txtCode.setText(_imei);

    }

    //监听返回键
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        if (System.currentTimeMillis() - currentBackPressedTime > 3000) {
            currentBackPressedTime = System.currentTimeMillis();
            Toast.makeText(getApplication(), "再按一次则退出", Toast.LENGTH_SHORT).show();
        } else {
            Bundle bundle = new Bundle();
            bundle.putBoolean("licensed", true);

            Intent intent = new Intent();
            intent.putExtras(bundle);
            AuthActivity.this.setResult(RESULT_CANCELED, intent);
            finish();
        }
    }

    public void onClickAuth(View view) {
        // 验证
        if (check_code()) {
            Bundle bundle = new Bundle();
            bundle.putBoolean("licensed", true);

            Intent intent = new Intent();
            intent.putExtras(bundle);
            AuthActivity.this.setResult(RESULT_OK, intent);
            finish();
        }
        else {
            Toast.makeText(getApplication(), "注册码不正确", Toast.LENGTH_SHORT).show();
        }
    }

    private void get_imei() {
        // 检查机器码是否已经存在
        String imei_w;
        SharedPreferences preferences = getSharedPreferences("Profile", MODE_PRIVATE);

        imei_w = preferences.getString("imei", null);

        if (imei_w == null) {
            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            try {
                String imei = tm.getDeviceId();
                // 修改授权码长度
                _imei = imei.substring(imei.length() - length_of_license, imei.length());
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "不能读取手机信息", Toast.LENGTH_LONG).show();
            }
            // 保存
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("imei", _imei);
            editor.apply();
        } else {
            _imei = imei_w;
        }
    }

    // 用户端检查输入的注册码
    private Boolean check_code() {
        String code_cal = MyMD5.MD5(_imei).substring(0, 6);
        String code = edAuthcode.getText().toString().trim();
        //Toast.makeText(getApplication(),_imei+","+code_cal+","+code, Toast.LENGTH_LONG).show();
        if (code.equals(code_cal)) {
            // 保存数据
            SharedPreferences.Editor editor = getSharedPreferences("Profile", MODE_PRIVATE).edit();
            editor.putBoolean("licensed", true);
            //editor.putString("imei", _imei);
            editor.putString("code", code);
            editor.apply();

            return true;
        }
        else {
            return false;
        }
    }

    //
}
